const express = require('express')
// const mongoose = require('mongoose')
const router = express.Router()
const User = require('../models/User')
const Category = require('../models/Category')
const Content = require('../models/Content')


//统一返回格式
var responseData = null;

router.use(function(req, res, next) {

  responseData = {
    code: 0,
    message: ''
  }
  next();
})

//用户注册
router.post('/user/register', function(req, res, next) {

  //赋值请求数据方便处理
  let username = req.body.username;
  let password = req.body.password;
  let repassword = req.body.repassword;

  //进行注册逻辑判断
  //1 用户名，密码，确认密码都不能为空
  //2 密码和确认密码必须一样
  //3 查询该用户名是否被注册了

  //1 用户名，密码，确认密码都不能为空
  if(!(username && password && repassword)) {
    responseData.code = 1;
    responseData.message = '请输入完整信息';
    res.json(responseData)
    return;
  }

  //2 密码和确认密码必须一样
  if(password != repassword) {
    responseData.code = 1;
    responseData.message = '两次输入的密码不一致';
    res.json(responseData);
    return
  } 

  //3 查询该用户名是否被注册了
  User.findOne({
    username: username
  }).then(function(userInfo) {
    if(userInfo){
      responseData.code = 1;
      responseData.message = '该用户已被注册'
      res.json(responseData);
      return;
    }
    let user = new User({
      username : username,
      password : password
    })
    user.save();
    responseData.message = '注册成功';
    res.json(responseData);
    return;
  })
});

//登录
router.post('/user/login', function(req, res, next) {

  //取得前端发来的数据
  let username = req.body.username;
  let password = req.body.password;
  //进行和数据库的核对
  User.findOne({
    username: username
  }).then(function(userInfo) {
    if(!userInfo) {
      responseData.code = 1;
      responseData.message = '用户名不存在';
      res.json(responseData);
      return;
    } else {
      if(userInfo.password != password) {
        responseData.code = 1;
        responseData.message = '密码错误';
        res.json(responseData);
        return;
      } else {
        responseData.code = 0;
        responseData.message = '登陆成功';
        responseData.userInfo = {
          _id: userInfo._id,
          username: userInfo.username
        }
        res.cookie('userInfo', {
          _id: userInfo._id,
          username: userInfo.username,
          isAdmin : userInfo.isAdmin
        }, {
          encode: String,
          path: '/',
          maxAge: 1000*60*60
        })
        res.json(responseData);
        return;
      }
    }
  })
})

//退出
router.get('/user/logout', function(req, res, next) {
  res.clearCookie('userInfo', {
    path: '/'
  })
  res.json(responseData)
  return;
})

//获取所有评论
router.get('/comments', function(req, res, next) {
  let contentId = req.query.contentId
  let page = req.query.page || 1

  //对数据提取进行拆分
  Content
    .findOne({
      _id: contentId
    })
    .then(function(content) {
      responseData.data = content
      responseData.page = page
      res.json(responseData)
      return
    })
})

//提交评论
router.post('/view/post', function(req, res, next) {
  let username = req.cookies.userInfo.username
  let contentId = req.body.contentId
  let comment = req.body.comment
  let page = req.body.page || 1
  Content
    .findOne({
      _id: contentId
    })
    .then(function(content) {
      content.comments.push({
        comment: comment,
        date: new Date(),
        user: username
      })
      return content.save()
    })
    .then(function(newContent) {
      responseData.message = '评论成功'
      responseData.data = newContent
      responseData.page = page
      res.json(responseData)
      return
    })
})


module.exports = router;
